package guowei.guliproduct.product.service.impl;

import guowei.guliproduct.product.dao.AttrAttrgroupRelationDao;
import guowei.guliproduct.product.entity.AttrAttrgroupRelationEntity;
import guowei.guliproduct.product.entity.AttrEntity;
import guowei.guliproduct.product.entity.CategoryEntity;
import guowei.guliproduct.product.service.AttrAttrgroupRelationService;
import guowei.guliproduct.product.service.AttrService;
import guowei.guliproduct.product.vo.AttrGroupAndRelations;
import guowei.guliproduct.product.vo.AttrRelationDelVo;
import guowei.guliproduct.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import guowei.common.utils.PageUtils;
import guowei.common.utils.Query;

import guowei.guliproduct.product.dao.AttrGroupDao;
import guowei.guliproduct.product.entity.AttrGroupEntity;
import guowei.guliproduct.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获取分类属性信息
     * 如果查询的属性是默认值0 则返回全部信息
     * {
     *  page: 1,//当前页码
     *  limit: 10,//每页记录数
     *  sidx: 'id",//排序字段
     *  order : "asc/desc",//排序方式
     *  key'华为·//检索关键字
     * }
     *
     *
     * @param params
     * @param catelogId
     * @return
     */
    @Override
    public PageUtils queryPageCatelogId(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");

        if(catelogId == 0){
            QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
            if(!StringUtils.isEmpty(key)){
                wrapper = new QueryWrapper<>();
                wrapper.eq("attr_group_id",key).or().like("attr_group_name",key);
            }
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper);
            return new PageUtils(page);
        }else {
//查询条件要是如下where category_id=? and (attr_group_id=key or attr_group_name like %key%)
            QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId);

//若传入的params的key不为空 就需要根据key查询指定信息
            if(!StringUtils.isEmpty(key)){
                wrapper.and((obj)->{
                    obj.eq("attr_group_id",key).or().like("attr_group_name",key);
                });
            }
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }
    }

    /**
     * 查询当前分组未关联的属性
     * @param attrGroupId
     * @param params
     * @return
     */
    @Override
    public PageUtils selectNoRelation(Long attrGroupId, Map<String, Object> params) {
        String key = (String) params.get("key");
        AttrGroupEntity entity = this.getOne(new QueryWrapper<AttrGroupEntity>().eq("attr_group_id", attrGroupId));
        Long catelogId = entity.getCatelogId();
        List<AttrEntity> list = attrService.list(new QueryWrapper<AttrEntity>().eq("catelog_id", catelogId));
        List<AttrEntity> entityList = list.stream().filter(attrEntity -> {
            AttrAttrgroupRelationEntity one = attrAttrgroupRelationService.getOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrEntity.getAttrId()));
            return one == null ? true : false;
        }).collect(Collectors.toList());
        QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("catelog_id",catelogId);
        IPage<AttrEntity> page = attrService.page(new Query<AttrEntity>().getPage(params), wrapper);
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(entityList);

        return pageUtils;
    }


    /**
     * 用于得到分类下分组所对应的规格参数属性信息
     * @param catelogId
     * @return
     */
    @Override
    public ArrayList<AttrGroupAndRelations> selectAttrGroupAndAttrRelations(Long catelogId) {

        //按老师的来
        List<AttrGroupEntity> list = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
//        List<AttrGroupAndRelations> collect = list.stream().map(attrGroupEntity -> {
//            AttrGroupAndRelations attrGroupAndRelations = new AttrGroupAndRelations();
//            BeanUtils.copyProperties(attrGroupEntity, attrGroupAndRelations);
//
//
//            return attrGroupAndRelations;
//        }).collect(Collectors.toList());


        List<AttrGroupAndRelations> relations = list.stream().map(attrGroupEntity -> {
            AttrGroupAndRelations attrGroupAndRelations = new AttrGroupAndRelations();
            BeanUtils.copyProperties(attrGroupEntity, attrGroupAndRelations);
            Long attrGroupId = attrGroupEntity.getAttrGroupId();
            List<AttrAttrgroupRelationEntity> list1 = attrAttrgroupRelationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrGroupId));
            List<AttrEntity> collect = list1.stream().filter(attrAttrgroupRelationEntity -> {
                Long attrId = attrAttrgroupRelationEntity.getAttrId();
                AttrEntity attr = attrService.getById(attrId);
                if (attr.getAttrType() == 1){
                    return true;
                }else {
                    return false;
                }
            }).map(attrAttrgroupRelationEntity -> {
                Long attrId = attrAttrgroupRelationEntity.getAttrId();
                AttrEntity attr = attrService.getById(attrId);
                return attr;
            }).collect(Collectors.toList());
            attrGroupAndRelations.setAttrs((ArrayList<AttrEntity>) collect);
            return attrGroupAndRelations;
        }).collect(Collectors.toList());
        return (ArrayList<AttrGroupAndRelations>) relations;
    }

    @Override
    public List<SpuItemAttrGroupVo> getThreeBuSpuId(Long cataLogId,Long spuId) {
        List<SpuItemAttrGroupVo> lists = attrGroupDao.getBuSpuId(cataLogId,spuId);
        return lists;
    }

    @Override
    public ArrayList<AttrEntity> getAttrAttrGroupTwo(Long attrgroupId) {
        List<AttrAttrgroupRelationEntity> entities = attrAttrgroupRelationService.list(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_group_id", attrgroupId));
        List<AttrEntity> collect = entities.stream().map((attrAttrgroupRelationEntity -> {
            AttrEntity attr = attrService.getById(attrAttrgroupRelationEntity.getAttrId());
            return attr;
        })).collect(Collectors.toList());
        return (ArrayList<AttrEntity>) collect;
    }

    /**
     * 关联界面删除 关联信息
     * @param
     */
    @Override
    public void deleteRelation(AttrRelationDelVo[] attrRelationDelVos) {
        List<AttrRelationDelVo> asList = Arrays.asList(attrRelationDelVos);
        List<AttrAttrgroupRelationEntity> list = asList.stream().map(attrRelationDelVo -> {
            AttrAttrgroupRelationEntity attrAttrgroupRelationEntity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(attrRelationDelVo, attrAttrgroupRelationEntity);
            return attrAttrgroupRelationEntity;
        }).collect(Collectors.toList());
        attrAttrgroupRelationDao.deleteBatch(list);
    }




}